package com.lancoo.answer.ui.child.lookReview;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lancoo.answer.R;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.widget.combinationView.CompositionAnswerResultView;
import com.lancoo.answer.widget.combinationView.QueseBodyView;

import java.util.List;

/**
 * Created by pengmi on 2021/12/8 11:52
 * Description:作文题查看评阅fragment
 */
public class CompositionLookReviewFragment extends Fragment {

    private Child child;
    private static final String TAG = "CompositionAnswerFragme";
    private CompositionAnswerResultView compositionAnswerResultView;
    private View convertView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        convertView= inflater.inflate(R.layout.ev_fragment_composition_answer_looking_layout, container, false);
        return convertView;
    }


    public static Fragment loadQueseFragment(int kindIndex, int queseIndex, int smallQueseIndex){

        CompositionLookReviewFragment queseFragment=new CompositionLookReviewFragment();
        Bundle mBundle=new Bundle();
        mBundle.putInt("kindIndex",kindIndex);
        mBundle.putInt("queseIndex",queseIndex);
        mBundle.putInt("smallQueseIndex",smallQueseIndex);
        queseFragment.setArguments(mBundle);
        return queseFragment;
    }

    public static Fragment loadChildFragment(int kindIndex, int queseIndex, int smallQueseIndex, Child child){

        CompositionLookReviewFragment queseFragment=new CompositionLookReviewFragment();
        Bundle mBundle=new Bundle();
        mBundle.putInt("kindIndex",kindIndex);
        mBundle.putInt("queseIndex",queseIndex);
        mBundle.putInt("smallQueseIndex",smallQueseIndex);
        queseFragment.setArguments(mBundle);
        return queseFragment;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view){
        Log.e(TAG,"initView"+(child==null));
        if(getArguments()==null){
            return;
        }
        int kindIndex = getArguments().getInt("kindIndex");
        int queseIndex = getArguments().getInt("queseIndex");
        int smallQueseIndex = getArguments().getInt("smallQueseIndex");

        if(ConstantBean.Companion.getConstantExamBean()==null){
            return;
        }
        List<Type> typeList = ConstantBean.Companion.getConstantExamBean().getTypeList();
        if(typeList==null){
            return;
        }
        List<Ques> quesList = typeList.get(kindIndex).getQuesList();
        if(quesList==null){
            return;
        }
        Ques quese = quesList.get(queseIndex);
        if( quese.getChildList()==null){
            return;
        }
        child = quese.getChildList().get(smallQueseIndex);
        QueseBodyView queseBodyView=view.findViewById(R.id.quese_body_view);
        queseBodyView.setEnableShowChildAsk(TextUtils.equals("X", quese.getTypeNo()));
        queseBodyView.initData(child);
        compositionAnswerResultView = view.findViewById(R.id.composition_answer_result_view);
        compositionAnswerResultView.setQueseID(quese.getQuesID());
        compositionAnswerResultView.setWriteResult(child, quese);

    }

    @Override
    public void onPause() {
        super.onPause();
        pauseAnswerAudioPlay();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(compositionAnswerResultView!=null){
            compositionAnswerResultView.release();
        }
    }

    public void pauseAnswerAudioPlay(){
        if(compositionAnswerResultView!=null){
            compositionAnswerResultView.pause();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if(compositionAnswerResultView!=null){
            compositionAnswerResultView.updateScoreState();
        }
    }

    public void adjustFontSize(){
        if(convertView==null){
            return;
        }
        QueseBodyView queseBodyView=convertView.findViewById(R.id.quese_body_view);
        queseBodyView.adjustFontSize();
        compositionAnswerResultView.adjustFontSize();
    }
}
